//
//  ExcelFirstColumnView.m
//  Common
//
//  Created by shiqing guo on 2018/10/17.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "ExcelFirstColumnView.h"
#import "ExcelTableDefine.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

@interface ExcelFirstColumnView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *headView;
@end

@implementation ExcelFirstColumnView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self addSubviews];
    }
    return self;
}

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.allowsSelection = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.userInteractionEnabled = NO;
        _tableView.bounces = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}


- (UIView *)headView
{
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, leftWidth, mHeight)];
        _headView.backgroundColor = [EPColor epColor_LineBlue];
        
        UILabel *lblCorner = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, leftWidth, mHeight-2)];
        lblCorner.backgroundColor = [EPColor epColor_BGBlue];
        lblCorner.text = @"编号";
        lblCorner.textColor = [EPColor epColor_Blue];
        lblCorner.font = SystemFont(16.f);
        lblCorner.textAlignment = NSTextAlignmentCenter;
        [_headView addSubview:lblCorner];
    }
    return _headView;
}

- (void)addSubviews {
    [self addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return mHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.headView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"ExcelLeftCell";
    ExcelLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ExcelLeftCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.lblColumnUnit.text = self.dataList[indexPath.row];
    return cell;
}


//同步左侧右侧
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
//    NSLog(@"左边偏移%f", offsetY);
}

#pragma mark - 数据
- (void)setDataList:(NSMutableArray *)dataList
{
    _dataList = dataList;
    CGFloat height = ((cellHeight*dataList.count + mHeight) > EPContentViewHeight-bottomSpace)?EPContentViewHeight-bottomSpace:(cellHeight*dataList.count + mHeight);
    self.tableView.height = height;
    self.tableView.contentSize = CGSizeMake(self.frame.size.width, cellHeight*dataList.count);
    [self.tableView reloadData];
}


@end

@implementation ExcelLeftCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [EPColor epColor_LineBlue];
        self.lblColumnUnit = [[UILabel alloc] initWithFrame:self.frame];
        self.lblColumnUnit.backgroundColor = [EPColor epColor_BGBlue];
        self.lblColumnUnit.textAlignment = NSTextAlignmentCenter;
        self.lblColumnUnit.textColor = [EPColor epColor_Blue];
        self.lblColumnUnit.font = SystemFont(14.f);
        [self.contentView addSubview:self.lblColumnUnit];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.lblColumnUnit.width = self.width;
    self.lblColumnUnit.height = self.height - 1;
}

@end
